package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.zzyl.entity.User;
import com.zzyl.mapper.UserMapper;
import com.zzyl.vo.UserAuth;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

/**
 * 自定义UserDetailService的实现类，
 * 目标是 为了重写loadUserByUserName方法，可以去数据库表中查询用户信息
 */
@Component
public class MyUserDetailServiceImpl implements UserDetailsService {

    @Autowired
    private UserMapper userMapper;
    /**
     *
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //1.调mapper接口
        User user = userMapper.findByUsername(username);
        if (null == user || user.getDataState().equals("1")) {
            throw new RuntimeException("用户登录失败");
        }
        //2.返回
        UserAuth userAuth = BeanUtil.toBean(user, UserAuth.class);
        return userAuth;
    }
}
